package com.yipao.db.service.order;

import com.github.pagehelper.PageHelper;
import com.yipao.db.dao.order.CartMapper;
import com.yipao.db.domain.order.Cart;
import com.yipao.db.domain.order.vo.CartExample;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 *
 * @author tianhao
 */
@Service
public class CartService {
    @Resource
    private CartMapper cartMapper;

    /**
     * 判断购物车是否存在相同,如果存在则累加
     *
     * @param goodsId        商品id
     * @param userId         用户
     * @param devId          设备id
     * @param formulaAttrMd5 商品配方属性的md5值
     * @return
     */
    public Cart queryExist(Integer goodsId, Integer userId, Integer devId, String formulaAttrMd5) {
        CartExample example = new CartExample();
        example.or().
                andGoodsIdEqualTo(goodsId).
                andUserIdEqualTo(userId).
                andDevIdEqualTo(devId).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED)
                .andFormulaAttrMd5EqualTo(formulaAttrMd5);
        return cartMapper.selectOneByExample(example);
    }

    public void add(Cart cart) {
        cart.setAddTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        cartMapper.insertSelective(cart);
    }

    public int updateById(Cart cart) {
        cart.setUpdateTime(LocalDateTime.now());
        return cartMapper.updateByPrimaryKeySelective(cart);
    }

    /**
     * 根据用户id和设备id查询购物车
     *
     * @param userId
     * @param devId
     * @return
     */
    public List<Cart> queryByUidDevId(int userId, Integer devId) {
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).
                andDevIdEqualTo(devId).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return cartMapper.selectByExample(example);
    }

    public List<Cart> queryByUid(int userId) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return cartMapper.selectByExample(example);
    }


    public List<Cart> queryByUidAndChecked(Integer userId) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return cartMapper.selectByExample(example);
    }

    public int delete(List<Integer> ids, int userId) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andIdIn(ids);
        return cartMapper.logicalDeleteByExample(example);
    }

    public Cart findById(Integer id) {
        return cartMapper.selectByPrimaryKey(id);
    }

    public Cart findById(Integer userId, Integer id) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).
                andIdEqualTo(id).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return cartMapper.selectOneByExample(example);
    }

    public List<Cart> findByIds(Integer userId, List<Integer> ids) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andIdIn(ids).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return cartMapper.selectByExample(example);
    }

    public int updateCheck(Integer userId, List<Integer> idsList, Boolean checked) {
        CartExample example = new CartExample();
        example.or().andUserIdEqualTo(userId).andIdIn(idsList).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        Cart cart = new Cart();
        cart.setChecked(checked);
        cart.setUpdateTime(LocalDateTime.now());
        return cartMapper.updateByExampleSelective(cart, example);
    }

    /**
     * 删除购物车
     *
     * @param userId  用户id
     * @param devId   设备id
     * @param cartIds 购物车id集合
     */
    public void clearGoods(Integer userId, Integer devId, List<Integer> cartIds) {
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        if (cartIds != null) {
            criteria.andIdIn(cartIds);
        }
        if (devId != null) {
            criteria.andDevIdEqualTo(devId);
        }
        Cart cart = new Cart();
        cart.setDeleted(DeleteStatusConstant.IS_DELETED);
        cartMapper.updateByExampleSelective(cart, example);
    }


    /**
     * 删除购物车,通过商品id
     *
     * @param goodsId 商品id
     */
    public void clearGoodsByGoodsId(Integer goodsId) {
        CartExample example = new CartExample();
        example.or().andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED).andGoodsIdEqualTo(goodsId);

        Cart cart = new Cart();
        cart.setDeleted(DeleteStatusConstant.IS_DELETED);
        cartMapper.updateByExampleSelective(cart, example);
    }

    public List<Cart> querySelective(Integer userId, Integer goodsId, Integer page, Integer limit, String sort, String order) {
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(goodsId)) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return cartMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        cartMapper.logicalDeleteByPrimaryKey(id);
    }

    public boolean checkExist(Integer goodsId) {
        CartExample example = new CartExample();
        example.or().andGoodsIdEqualTo(goodsId).
                andCheckedEqualTo(true).
                andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return cartMapper.countByExample(example) != 0;
    }

    public void updateProduct(Integer id, String goodsSn, String goodsName, BigDecimal price, String url) {
        Cart cart = new Cart();
        cart.setPrice(price);
        cart.setPicUrl(url);
        cart.setGoodsSn(goodsSn);
        cart.setGoodsName(goodsName);
        CartExample example = new CartExample();
        example.or().andProductIdEqualTo(id);
        cartMapper.updateByExampleSelective(cart, example);
    }

    public List<Cart> findByUid(Integer userId) {
        CartExample example = new CartExample();
        CartExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return cartMapper.selectByExample(example);
    }
}
